
$(function(){

    var url='/getAccountlistData';
    var start_date='';
    var end_date='';
    var username='';
    var manage_name='';

    var sort_name = 'created_at';
    var sort_value = 'desc';
    var level = '';
    var cur = 0;
    var type = 'order';
    var expired ='';
    var set_type='';
    var acc_type='';

    var listData = new Vue({
        el: '#app',
        data:{
            level:0,
            account:{},
            discount:{},
            count:0,
            all:0,
            cur:1,
        },
        created:function(){
            this.fetchData(url,this.cur,start_date,end_date,username,manage_name);
        },
        methods: {
            fetchData: function(url,cur,start_date,end_date,username,manage_name) {
                var self  = this;
                $.ajax({
                    type: "get",
                    //url: url+'?page='+cur+'&start_date='+start_date+'&end_date='+end_date+'&username='+username+'&manage_name='+manage_name,
                    url: url+'?page='+cur+'&start_date='+start_date+'&end_date='+end_date+'&username='+username+
                    '&manage_name='+manage_name+'&sort_name='+sort_name+'&sort_value='+sort_value+'&type='+type+'&expired_at='+expired+
                    '&set_meal_type='+set_type+'&account_type='+acc_type,
                    async: true,
                    dataType:'json',
                    success: function(data) {
                        self.account = data.data.account;
                        self.discount = data.data.discount;
                        self.count = data.data.count;
                        self.level = data.data.level;
                        self.all = data.data.page_total;
                    },
                });
            },
            btnClick: function(data){//页码点击事件
                if(data != this.cur){
                    this.cur = data;
                    this.fetchData(url,this.cur,start_date,end_date,username,manage_name);
                }
            },
            pageClick: function(){
                this.fetchData(url,this.cur,start_date,end_date,username,manage_name);

            },
            fromClick: function(){

                expired =  $("input[name='expired_at']:checked").val();
                set_type = $("input[name='set_meal_type']:checked").val();
                acc_type = $("input[name='account-type']:checked").val();

                start_date = $('#start_date').val();
                end_date = $('#end_date').val();
                username = $('#username').val();
                manage_name = $('#manage_name').val();

                this.fetchData(url,this.cur,start_date,end_date,username,manage_name);
            },
            renewClick: function(e,id,connect_num,username,password,set_meal_type){

                if(set_meal_type ==1 ){
                    $('#renew-day').val(primary_day);
                    $('#renew-week').val(primary_week);
                    $('#renew-month').val(primary_month);
                }


                if(set_meal_type ==2 ){
                    $('#renew-day').val(middle_day);
                    $('#renew-week').val(middle_week);
                    $('#renew-month').val(middle_month);
                }

                if(set_meal_type ==3 ){
                    $('#renew-day').val(height_day);
                    $('#renew-week').val(height_week);
                    $('#renew-month').val(height_month);
                }


                $('#connect-num').val(connect_num);
                $('#data-name').val(username);
                $('#data-id').val(id);
                $('#data-password').val(password);

                this.checkRenewMoney();
            },
            checkRenewMoney:function(){
                var renew_date = parseInt($('#renew-date').val());
                var connect_num = parseInt($('#connect-num').val());
                var discount = $('#renew_type option:selected').val();

                if($('#renew-date').val() =="")
                {
                    $('.renew_price').text('0.00');
                    $('#data-price').val(0)

                } else{

                    $('.renew_price').text((renew_date*connect_num*discount));
                    $('#data-price').val((renew_date*connect_num*discount));
                    $('#data-type').val($("#renew_type").find("option:selected").text());

                }
            },
            accountUpdate:function(e,id,value,password,username,remark) {

                $('#edit-name').val(username);
                $('#edit-password').val(password);
                $('#edit-speed').val(value);
                $('#edit-id').val(id);
                $('#edit-remark').val(remark);
            },
            accountDelete:function(e,id){
                var self = this;
                if(confirm('真的删除此账号,删除以后该账号信息将不存在,请谨慎操作')) {
                    $_this = e;
                    $.ajax({
                        url: '/delete/account',          //  删除账号
                        type: 'get',
                        data: {
                            id: id
                        },
                        timeout: 5000,
                        dataType: 'json',
                        success: function (data, textStatus, jqXHR) {
                            if (data.error_code == 0) {
                                if (data.data == 'SUCCESS') {
                                    self.account.splice(e,1);
                                    //e.parent().parent().hide();
                                    alert('删除成功');
                                }
                            } else {
                                alert(data.error_message);
                            }
                        }
                    })

                }
            },
        },
        computed: {
            indexs: function(){
                var left = 1;
                var right = this.all;
                var ar = [];
                if(this.all>= 5){
                    if(this.cur > 3 && this.cur < this.all-2){
                        left = this.cur - 2
                        right = this.cur + 2
                    }else{
                        if(this.cur<=3){
                            left = 1
                            right = 5
                        }else{
                            right = this.all
                            left = this.all -4
                        }
                    }
                }
                while (left <= right){
                    ar.push(left)
                    left ++
                }
                return ar
            }

        }

    });


        $('.sort-up').click(function(){


            sort_name =$(this).attr('data-name');
            sort_value =$(this).attr('data-value');
            type =$(this).attr('data-type');

            expired =  $("input[name='expired_at']:checked").val();
            set_type = $("input[name='set_meal_type']:checked").val();
            acc_type = $("input[name='account-type']:checked").val();

            start_date = $('#start_date').val();
            end_date = $('#end_date').val();
            username = $('#username').val();
            manage_name = $('#manage_name').val();

            listData.fetchData(url,cur,start_date,end_date,username,manage_name,sort_name,sort_value,type);
        });

        $('.sort-desc').click(function(){

            sort_name =$(this).attr('data-name');
            sort_value =$(this).attr('data-value');
            type =$(this).attr('data-type');

            expired =  $("input[name='expired_at']:checked").val();
            set_type = $("input[name='set_meal_type']:checked").val();
            acc_type = $("input[name='account-type']:checked").val();

            start_date = $('#start_date').val();
            end_date = $('#end_date').val();
            username = $('#username').val();
            manage_name = $('#manage_name').val();
            new listData.fetchData(url,cur,start_date,end_date,username,manage_name,sort_name,sort_value,type);

        });




});
